package com.jzy.bxb.model.util;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.jzy.bxb.core.util.MathUtils;
import com.jzy.bxb.model.constant.AppConstant;
import com.jzy.bxb.model.struct.SuggestReplayTemplate;
import com.jzy.bxb.model.struct.VerificationTemplate;

/**
 * 短信发送
 * 
 * @author JiangZhiYong QQ:359135103
 */
public class SMSUtil {
	private static final Logger LOGGER = LoggerFactory.getLogger(SMSUtil.class);

	
	/**
	 * 发送 验证码
	 */
	public static int sendSuggestReplay(String content, String phone) {
		int code = 0;
		try {
			Map<String, String> map = new TreeMap<>();
			map.put("showapi_appid", String.valueOf(AppConstant.VERIFICATION_CODE_APPID));
			map.put("mobile", phone);
			map.put("tNum", AppConstant.SUGGEST_REPLAY_CODE_TNUM);
			code = MathUtils.random(100000, 999999);
			map.put("content", JSON.toJSONString(new SuggestReplayTemplate(content)));

			StringBuffer signParams = new StringBuffer();
			StringBuilder url = new StringBuilder(AppConstant.VERIFICATION_CODE_URL);
			url.append("?");

			map.forEach((key, value) -> {
				signParams.append(key).append(value);
				url.append(key).append("=").append(value).append("&");
			});

			signParams.append(AppConstant.VERIFICATION_CODE_SECRET);
			// System.err.println(signParams.toString());

			String md5Hex = DigestUtils.md5Hex(signParams.toString().getBytes("UTF-8"));
			// System.err.println(md5Hex);
			url.append("showapi_sign=").append(md5Hex);
			// System.err.println(url.toString());

			map.put("showapi_sign", md5Hex);

			URL u = new URL(url.toString());
			InputStream in = u.openStream();
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			try {
				byte buf[] = new byte[1024];
				int read = 0;
				while ((read = in.read(buf)) > 0) {
					out.write(buf, 0, read);
				}
			} finally {
				if (in != null) {
					in.close();
				}
			}
			byte b[] = out.toByteArray();
			System.out.println(new String(b, "utf-8"));
		} catch (Exception e) {
			LOGGER.warn("sendVerificationCode", e);
		}
		return code;
	}
	
	/**
	 * 发送 验证码
	 */
	public static int sendVerificationCode(String name, String phone) {
		int code = 0;
		try {
			Map<String, String> map = new TreeMap<>();
			map.put("showapi_appid", String.valueOf(AppConstant.VERIFICATION_CODE_APPID));
			map.put("mobile", phone);
			map.put("tNum", AppConstant.VERIFICATION_CODE_TNUM);
			code = MathUtils.random(100000, 999999);
			map.put("content", JSON.toJSONString(new VerificationTemplate(name, String.valueOf(code),
					String.valueOf(AppConstant.VERIFICATION_CODE_TIME))));

			StringBuffer signParams = new StringBuffer();
			StringBuilder url = new StringBuilder(AppConstant.VERIFICATION_CODE_URL);
			url.append("?");

			map.forEach((key, value) -> {
				signParams.append(key).append(value);
				url.append(key).append("=").append(value).append("&");
			});

			signParams.append(AppConstant.VERIFICATION_CODE_SECRET);
			// System.err.println(signParams.toString());

			String md5Hex = DigestUtils.md5Hex(signParams.toString().getBytes("UTF-8"));
			// System.err.println(md5Hex);
			url.append("showapi_sign=").append(md5Hex);
			// System.err.println(url.toString());

			map.put("showapi_sign", md5Hex);

			URL u = new URL(url.toString());
			InputStream in = u.openStream();
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			try {
				byte buf[] = new byte[1024];
				int read = 0;
				while ((read = in.read(buf)) > 0) {
					out.write(buf, 0, read);
				}
			} finally {
				if (in != null) {
					in.close();
				}
			}
			byte b[] = out.toByteArray();
			System.out.println(new String(b, "utf-8"));
		} catch (Exception e) {
			LOGGER.warn("sendVerificationCode", e);
		}
		return code;
	}
}
